package cn.xkai.exercise.b;

/**
 * @description: 位1的个数
 * 移位技巧
 * @author: kaixiang
 * @date: 2022-07-14
 **/
public class Solution60 {
    public int hammingWeight(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            System.out.println(n >>> i);
            if (((n >>> i) & 1) == 1) {
                count++;
            }
        }
        return count;
    }

    public int hammingWeightSimple(int n) {
        int count = 0;
        while (n != 0) {
            count += n & 1;
            n = n >>> 1;
        }
        return count;
    }

    public int hammingWeightGeneral(int n) {
        int count = 0;
        while (n != 0) {
            n &= n - 1;
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        int n = 3;
        Solution60 solution60 = new Solution60();
        System.out.println(solution60.hammingWeight(n));
    }
}
